package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    @Override
    public ResponseResult findAll() {

        return ResponseResult.okResult(list());
    }

    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Override
    public ResponseResult insert(WmChannel wmChannel) {
        if (wmChannel == null || wmChannel.getOrd() < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        String name = wmChannel.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "频道名不能为空");
        }

        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getName, name);
        Integer count = wmChannelMapper.selectCount(wrapper);
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道不能重名");
        }

        wmChannel.setCreatedTime(new Date());
        boolean saveResult = this.save(wmChannel);

        return saveResult ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);

    }

    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        if (dto == null) {
            return new PageResponseResult(0, 0, 0);
        }

        Integer page = dto.getPage();
        Integer size = dto.getSize();

        page = page == null ? 1 : page;
        size = size == null ? 10 : size;

        Page<WmChannel> pageInfo = new Page<>(page, size);

        String name = dto.getName();
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name), WmChannel::getName, name);
        wrapper.orderByDesc(WmChannel::getCreatedTime);

        Page<WmChannel> pageResult = wmChannelMapper.selectPage(pageInfo, wrapper);

        Long total = pageResult.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(page, size, total.intValue());
        pageResponseResult.setData(pageResult.getRecords());

        return pageResponseResult;
    }


    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Override
    public ResponseResult update(WmChannel wmChannel) {
        if (wmChannel == null || wmChannel.getOrd() < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        LambdaQueryWrapper<WmNews> newsWrapper = new LambdaQueryWrapper<>();
        newsWrapper.eq(WmNews::getChannelId, wmChannel.getId());
        newsWrapper.eq(WmNews::getStatus, 9);
        Integer newsCount = wmNewsMapper.selectCount(newsWrapper);
        if (newsCount > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "频道关联了新闻，无法修改");
        }

        boolean updateResult = this.updateById(wmChannel);

        return updateResult ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);

    }

    @Override
    public ResponseResult delete(Integer id) {

        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if (wmChannel == null) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        Boolean status = wmChannel.getStatus();
        if (status) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道为启动状态，不能删除");
        }

        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId, wmChannel.getId());
        wrapper.eq(WmNews::getStatus, 9);
        Integer newsCount = wmNewsMapper.selectCount(wrapper);
        if (newsCount > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "频道关联了新闻，无法修改");
        }


        boolean removeResult = this.removeById(id);
        return removeResult ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}